import functinn as fun
import numpy as np
import scipy.io as sio

data = sio.loadmat('ex3data1.mat')
raw_x = data['X']
raw_y = data['y']
X = np.insert(raw_x, 0, values=1, axis=1)
y = raw_y.flatten()
print(f"X的维度:{X.shape},y的维度:{y.shape}")

theta = sio.loadmat('ex3weights.mat')
theta1 = theta['Theta1']
theta2 = theta['Theta2']
print(f"theta1的维度:{theta1.shape},theta2的维度:{theta2.shape}")

# 输入层
a1 = X

# 隐藏层
z2 = a1 @ theta1.T
# 激活函数
a2 = fun.sigmoid(z2)

# 输出层
# 加入偏置层
alpha = np.insert(a2, 0, values=1, axis=1)
z3 = alpha @ theta2.T
a3 = fun.sigmoid(z3)

# 正确率检测
y_pre = np.argmax(a3, axis=1)
y_pre = y_pre + 1
acc = np.mean(y_pre == y)

print(acc)
